<template>
  <div class="comment-list">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="暂无评论数据"
      @load="onLoadComment"
    >
    <comment-item v-for="(item,index) in list" :comment="item" :key="index" @reply-click="$emit('reply-click',$event)"></comment-item>
    </van-list>
  </div>
</template>

<script>
import CommentItem from './CommentItem.vue'
import { getComments } from '@/apis/comment'
export default {
  name: 'CommentList',
  props: {
    source: {
      type: [Number, String, Object],
      required: true
    },
    type: {
      type: String,
      default: 'a'
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  components: {
    CommentItem
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },
  methods: {
    async onLoadComment () {
      const { data } = await getComments({
        type: this.type,
        source: this.source.toString(),
        offset: this.offset,
        limit: this.limit
      })
      const { results } = data.data
      this.list.push(...results)
      // 加载状态结束
      this.loading = false
      // 评论数量
      this.$emit('update-count', data.data.total_count)
      if (results.length > 0) {
        this.offset = data.data.last_id// 更新页面
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="less" scoped>

</style>
